﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace wpf_nes
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class Control_Nes_View : UserControl
    {
        public Control_Nes_View()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            staticfunc.Start();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.mainimg.Source = staticfunc.frameWpf;
            //myid = 0;
            //DispatcherTimer dispatcherTimer = new DispatcherTimer();
            //dispatcherTimer.Tick += new EventHandler(CheckUpdate);
            //dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 10);
            //dispatcherTimer.Start();
        }

        private void Button_KeyDown(object sender, KeyEventArgs e)
        {
        }
        private void Button_KeyUp(object sender, KeyEventArgs e)
        {

        }
        //selectdown



        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var data =System.IO.File.ReadAllBytes("d:\\2.nes");
            Plugin._IDE.SetData_ByteArray("__curImage", data);
            staticfunc.Start();
            staticfunc.PressButton(Nescafe.Controller.Button.Select);

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            staticfunc.PressButton(Nescafe.Controller.Button.Start);
        }



        private void Mainimg_KeyDown(object sender, KeyEventArgs e)
        {
            bool state = true;
            switch (e.Key)
            {
                case Key.W:
                    staticfunc.console.Controller.setButtonState(Nescafe.Controller.Button.Up, state);
                    break;
                case Key.S:
                    staticfunc.console.Controller.setButtonState(Nescafe.Controller.Button.Down, state);
                    break;
                case Key.A:
                    staticfunc.console.Controller.setButtonState(Nescafe.Controller.Button.Left, state);
                    break;
                case Key.D:
                    staticfunc.console.Controller.setButtonState(Nescafe.Controller.Button.Right, state);
                    break;
                case Key.J:
                    staticfunc.console.Controller.setButtonState(Nescafe.Controller.Button.B, state);
                    break;
                case Key.K:
                    staticfunc.console.Controller.setButtonState(Nescafe.Controller.Button.A, state);
                    break;
                case Key.Space:
                    staticfunc.console.Controller.setButtonState(Nescafe.Controller.Button.Select, state);
                    break;
                case Key.Enter:
                    staticfunc.console.Controller.setButtonState(Nescafe.Controller.Button.Start, state);
                    break;
            }


        }

        private void Mainimg_KeyUp(object sender, KeyEventArgs e)
        {
            bool state = false;
            switch (e.Key)
            {
                case Key.W:
                    staticfunc.console.Controller.setButtonState(Nescafe.Controller.Button.Up, state);
                    break;
                case Key.S:
                    staticfunc.console.Controller.setButtonState(Nescafe.Controller.Button.Down, state);
                    break;
                case Key.A:
                    staticfunc.console.Controller.setButtonState(Nescafe.Controller.Button.Left, state);
                    break;
                case Key.D:
                    staticfunc.console.Controller.setButtonState(Nescafe.Controller.Button.Right, state);
                    break;
                case Key.J:
                    staticfunc.console.Controller.setButtonState(Nescafe.Controller.Button.B, state);
                    break;
                case Key.K:
                    staticfunc.console.Controller.setButtonState(Nescafe.Controller.Button.A, state);
                    break;
                case Key.Space:
                    staticfunc.console.Controller.setButtonState(Nescafe.Controller.Button.Select, state);
                    break;
                case Key.Enter:
                    staticfunc.console.Controller.setButtonState(Nescafe.Controller.Button.Start, state);
                    break;
            }

        }

        private void Mainimg_MouseDown(object sender, MouseButtonEventArgs e)
        {
            mainimg.Focus();
        }
    }
}
